package com.lql.linkedtable;

/**
 * 约瑟夫问题
 */
public class ProsepFu {

    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.countBoy(10, 20, 125);
    }
}

class CircleSingleLinkedList {
    // 首先定义一个第一个节点,使用-1表示链表为空
    private Boy first = new Boy(-1);

    /**
     * 根据要添加的个数，新建一个环形单向链表
     *
     * @param num 要创建的节点个数
     */
    public void addBoy(int num) {
        // num数据校验
        if (num < 1) {
            throw new RuntimeException("num数值异常，请输入大于1的数字");
        }
        // 定义一个辅助指针
        Boy currentBoy = null;
        // 循环创建循环链表
        for (int i = 1; i <= num; i++) {
            Boy boy = new Boy(i);
            // 判断是否为第一个节点
            if (i == 1) {
                first = boy;
                first.setNext(first);
                currentBoy = first;
            }
            currentBoy.setNext(boy);
            boy.setNext(first);
            currentBoy = boy;
        }
    }

    /**
     * 展示循环单链表节点信息
     */
    public void showCircleLinkedList() {
        if (first.getNo() == -1) {
            // 链表为空
            System.out.println("链表为空");
            return;
        }
        // 定义一个辅助指针
        Boy currentBoy = first;
        while (true) {
            //打印节点信息
            System.out.println(currentBoy);
            if (currentBoy.getNext() == first) {
                //遍历结束
                break;
            }
            // 指针下移一位
            currentBoy = currentBoy.getNext();
        }
    }

    /**
     * 约瑟夫问题解决
     *
     * @param startNum 开始报数的个数 k
     * @param countNum 报数几次出圈 m
     * @param num      总人数 n
     */
    public void countBoy(int startNum, int countNum, int num) {
        //创建队列
        addBoy(num);
        if (first.getNo() == -1) {
            // 链表为空
            System.out.println("链表为空");
            return;
        }
        // 数据校验
        if (startNum > num || countNum > num || countNum < 0 || startNum < 0) {
            throw new RuntimeException("数据传值异常");
        }
        // 定义辅助指针
        Boy helper = first;
        // 使辅助指针指向尾节点
        while (helper.getNext() != first) {
            helper = helper.getNext();
        }
        //移动指针和first节点到开始报数位置
        for (int i = 0; i < startNum - 1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        // 开始报数
        //链表中只剩下一个节点
        while (first != helper) {
            for (int i = 0; i < countNum - 1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            System.out.println(first);
            //first = first.next
            first = first.getNext();
            // helper.next = first
            helper.setNext(first);
        }
        //处理最后一个节点
        System.out.println("最后一个出圈的小孩是：" + first);
    }
}

class Boy {
    private int no;
    private Boy next;

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "no=" + no +
                '}';
    }
}
